
package memsim;

/**
 *
 * @author Alan
 */
public class RegisterHandler {


    private Register genRegisters; //register instance for the general purpose registers
    final int SP = 12; //stack pointer
    final int PC = 13; //program counter
    final int SR = 14; //status register
    final int LR = 15; //link register
    final int PCINCREMENT = 4; //how much to increment the PC by

    /**
     * Constructor that just initailizes the instances for theneeded register sets
     */
    public RegisterHandler()
    {
        genRegisters = new Register(16,0,32,true);
    }

    /**
     * Method to get the value from a specific register
     *
     * @param regNum    number of the register
     * @return          int value in the register
     */
    public int getValue(int regNum)
    {
        return genRegisters.getValue(regNum);
    }

    /**
     * Method to get value of the program counter
     *
     * @return          int value in the Program Counter
     */
    public int getPC()
    {
        return genRegisters.getValue(PC);
    }

    /**
     * Method to get value of the Link Register
     *
     * @return          int value in the Link Register
     */
    public int getLR()
    {
        return genRegisters.getValue(LR);
    }

    /**
     * Method to get value of the Stack Pointer
     *
     * @return          int value in the Stack Pointer
     */
    public int getSP()
    {
        return genRegisters.getValue(SP);
    }

    /**
     * Method to get value of the Status Register
     *
     * @return          int value in the Status Register
     */
    public int getSR()
    {
        return genRegisters.getValue(SP);
    }

    /**
     * Method ot set the value in a specific register
     *
     * @param regNum    number of the register
     * @param value     value to be inserted into the register
     */
    public void setValue(int regNum, int value)
    {
        genRegisters.setValue(regNum, value);
        System.out.println("setting value " + value + " in " + regNum);
    }

    /**
     * put a value into a register after converting from hex
     *
     * @param regNum    number of the register the value will go into
     * @param value     the value being put into the register
     */
    public void setValue(int regNum, String value)
    {
        String[] str = value.toString().split("0x");

        int parsed = Integer.parseInt(str[1], 16);
        genRegisters.setValue(regNum, parsed);
    }

    /**
     * Method ot set the value into the Program Counter
     *
     * @param value     value to be inserted into the Program Counter
     */
    public void setPC(int value)
    {
        genRegisters.setValue(PC, value);
    }

    /**
     * Method ot set the value into the Link Register
     *
     * @param value     value to be inserted into the Link Register
     */
    public void setLR(int value)
    {
        genRegisters.setValue(LR, value);
    }

    /**
     * Method ot set the value into the Stack Pointer
     *
     * @param value     value to be inserted into the Stack Pointer
     */
    public void setSP(int value)
    {
        genRegisters.setValue(SP, value);
    }

    /**
     * Method ot set the value into the Status Register
     *
     * @param value     value to be inserted into the Status Register
     */
    public void setSR(int value)
    {
        genRegisters.setValue(SR, value);
    }

    /**
     * Method to increment the Program Counter
     */
    public void incPC()
    {
        setPC(genRegisters.getValue(PC) + PCINCREMENT);
    }

    public int[] getGenRegs()
    {
        return genRegisters.getRegData();
    }
    public Object[][] getTableArray()
    {
        int[] temp = genRegisters.getRegData();
        Object[][] table = new Object[16][2];
        for(int i = 0; i < 16; i++)
        {
            table[i][1] = temp[i];
        }
        table[0][0] = "R0";
        table[1][0] = "R1";
        table[2][0] = "R2";
        table[3][0] = "R3";
        table[4][0] = "R4";
        table[5][0] = "R5";
        table[6][0] = "R6";
        table[7][0] = "R7";
        table[8][0] = "R8";
        table[9][0] = "R9";
        table[10][0] = "R10";
        table[11][0] = "R11";
        table[12][0] = "R12";
        table[13][0] = "R13";
        table[14][0] = "R14";
        table[15][0] = "R15";

        return table;

    }


}
